<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-22
 * Time: 18:50
 * 退款操作
 */

namespace app\api\service;


use app\api\model\Assistant;
use app\api\model\AssistantMoney;
use app\api\model\CityPartner;
use app\api\model\CityPartnerMoney;
use app\api\model\CountyPartner;
use app\api\model\CountyPartnerMoney;
use app\api\model\HotelRoom;
use app\api\model\HotelRoomOrder;
use app\api\model\ProvincePartner;
use app\api\model\ProvincePartnerMoney;
use app\lib\exception\ParameterException;

class AdminWxRefund extends Token
{
    /**
     * 1.判断用户信息
     * 2.判断订单信息
     * 3.判断订单时间 得出退款金额进行退款操作
     */
    // 用户ID
    private $postArr;

    // 订单信息
    private $order;

    // 房间信息
    private $hotelRoom;

    // 退款金额
    private $price;

    private $status;

    function __construct($postArr)
    {
        $this->postArr = $postArr;
        $this->order = HotelRoomOrder::get($this->postArr['order_id']);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeOrder();
    }

    // 判断订单
    private function judgeOrder()
    {
        if($this->postArr['string'] != 'N#l0owmG82aDGxH') {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '订单不存在'
                ]
            ]);
        }
        if($this->order['status'] == 3) {
            $this->status = 6;
        } elseif($this->order['status'] == 5) {
            $this->status = 7;
        } else {
            throw new ParameterException([
                'msg' => [
                    'room_order' => '订单状态错误'
                ]
            ]);
        }
        return $this->judgeData();
    }

    // 判断时间并得出退款金额
    private function judgeData()
    {
        $this->price = $this->postArr['price'];
        if($this->price == 0) {
           $this->changeStatus();
            return writeJson(201, [], '退款成功！');
        } else {
           return $this->orderRefund();
        }
    }

    // 进行退款操作
    private function orderRefund()
    {
        $wxOrderData = new \WxPayRefund();
        $wxOrderData->SetOut_trade_no($this->order['out_trade_no']);
        $wxOrderData->SetOut_refund_no($this->order['out_refund_no']);
        $wxOrderData->SetTotal_fee((int)(round($this->order['price'],2)*100));
        $wxOrderData->SetRefund_fee((int)(round($this->price,2)*100));
        $wxOrderData->SetOp_user_id(config('wx.op_user_id'));
        $config = new \WxPayConfig();
        $order_info = \WxPayApi::refund($config, $wxOrderData);
        if ($order_info['return_code'] == 'SUCCESS' && $order_info['result_code'] == 'SUCCESS') {
            $this->changeStatus();
            return writeJson(201, [], '退款成功！');
        } else {
            throw new ParameterException([
                'msg' => [
                    'room_order' => $order_info['err_code_des']
                ]
            ]);
        }
    }

    // 修改状态
    private function changeStatus()
    {
        HotelRoomOrder::where('id',$this->order['id'])->update([
            'send_time' => time(),
            'status' => $this->status,
            'refund_price' => $this->price,
            'refund_lin_user_id' => $this->postArr['lin_user_id']
        ]);
        $this->hotelRoom = HotelRoom::get($this->order['hotel_room_id']);
        $this->provinceCommission();
        $this->cityCommission();
        $this->countyCommission();
    }


    // 省代返佣操作
    private function provinceCommission()
    {
        $ProvincePartner = ProvincePartner::where('province_id',$this->hotelRoom['province_id'])
            ->where('status',2)
            ->find();
        if($ProvincePartner) {
            if($ProvincePartner['commission'] > 0) {
                $invite_money = $ProvincePartner['money'] + round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2);
                $ProvincePartner->money = $invite_money;
                $ProvincePartner->save();
                if(round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2) > 0) {
                    ProvincePartnerMoney::create([
                        'province_partner_id' => $ProvincePartner['id'],
                        'invite_id' => $this->order['user_id'],
                        'money' => round($ProvincePartner['invite_money']/100 * $ProvincePartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'province_partner_money' => $ProvincePartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$ProvincePartner['id'])
                ->where('status',1)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
                $this->assistantCommission($v,$ProvincePartner);
            }
        }
    }

    // 市代返佣操作
    private function cityCommission()
    {
        $CityPartner = CityPartner::where('city_id',$this->hotelRoom['city_id'])
            ->where('status',2)
            ->find();
        if($CityPartner) {
            if($CityPartner['commission'] > 0) {
                $invite_money = $CityPartner['money'] + round($this->order['price'] * $CityPartner['commission'],2);
                $CityPartner->money = $invite_money;
                $CityPartner->save();
                if(round($this->order['price'] * $CityPartner['commission'],2) > 0) {
                    CityPartnerMoney::create([
                        'city_partner_id' => $CityPartner['id'],
                        'invite_id' => $this->order['user_id'],
                        'money' => round($this->order['price'] * $CityPartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'city_partner_money' => $CityPartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$CityPartner['id'])
                ->where('status',2)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
                $this->assistantCommission($v,$CityPartner);
            }
        }
    }

    // 县代返佣操作
    private function countyCommission()
    {
        $CountyPartner = CountyPartner::where('county_id',$this->hotelRoom['county_id'])
            ->where('status',2)
            ->find();
        if($CountyPartner) {
            if($CountyPartner['commission'] > 0) {
                $invite_money = $CountyPartner['money'] + round($this->order['price'] * $CountyPartner['commission'],2);
                $CountyPartner->money = $invite_money;
                $CountyPartner->save();
                if(round($this->order['price'] * $CountyPartner['commission'],2) > 0) {
                    CountyPartnerMoney::create([
                        'invite_id' => $this->order['user_id'],
                        'county_partner_id' => $CountyPartner['id'],
                        'money' => round($this->order['price'] * $CountyPartner['commission'],2),
                        'order_no' => $this->order['order_no'],
                        'county_partner_money' => $CountyPartner['money']
                    ]);
                }
            }
            $list = Assistant::where('pid',$CountyPartner['id'])
                ->where('status',3)
                ->select()->toArray();
            foreach ($list as $k=>$v) {
                $this->assistantCommission($v,$CountyPartner);
            }
        }
    }

    // 助理返佣
    private function assistantCommission($info,$partner)
    {
        if($info) {
            if($partner['invite_money'] > 0) {
                $money = $info['money'] + round($partner['invite_money']/100 * $partner['assistant_commission'],2);
                Assistant::where('id',$info['id'])->update([
                    'money' => $money
                ]);
                AssistantMoney::create([
                    'invite_id' => $this->order['user_id'],
                    'assistant_id' => $info['id'],
                    'money' => round($partner['invite_money']/100 * $partner['assistant_commission'],2),
                    'order_no' => $this->order['order_no'],
                    'assistant_money' => $info['money']
                ]);
            }
        }
    }

}